<template>
  <div class="evaluation-wrapper">
    <left-menu></left-menu>
    <div class="evaluation-content" ref="evaluationWrapper">
      <ccw-select @set-common="setCommonToNull" :select-condition='ccwCondition' :pp-data="ccwPPList" component-id="ccwDataSelect" cur-type="1" title="车虫网" @get-search-condition="getSearchCondition"></ccw-select>
      <ccw-select @set-common="setCommonToNull" :select-condition='cthCondition' :pp-data="cthPPList" component-id="cthDataSelect" cur-type="2" title="车300" @get-search-condition="getSearchCondition"></ccw-select>
      <ccw-select @set-common="setCommonToNull" :select-condition='escfCondition' :pp-data="escFPPList" component-id="escfDataSelect" cur-type="3" title="二手车之家" @get-search-condition="getSearchCondition"></ccw-select>
      <!--<cth-select :pp-data="cthPPList" component-id="cthDataSelect" cur-type="2" title="车300" :citys="cthCity" @get-search-condition="getSearchCondition"></cth-select>
                    <escf-select :pp-data="escFPPList" component-id="escfDataSelect" cur-type="3" title="二手车之家" :citys="escfCity" @get-search-condition="getSearchCondition"></escf-select>-->
      <common-condition v-show="showCommonSelect" :city-list="cityList" :common-condition="commonCondition" :ccw-condition="ccwCondition" :cth-condition="cthCondition" :escf-condition="escfCondition" @get-common-condition="getCommonCondition"></common-condition>
      <div class="zanCunData1"></div>
      <div class="zanCunData2"></div>
      <div class="zanCunData3"></div>
      <div class="zanCunData4"> </div>
      <div class="evalute-btn">
        <button v-show="showCommonSelect" class="btn" @click="judgeInfo">去估价</button>
        <!--<button class="btn" @click="test">测试</button>-->
      </div>
    </div>
    <!--<div v-if="showResult">
                    <result-panel :ccw="ccwEvaData" :cth="cthEvaData" :escf="escfEvaData"></result-panel>
                  </div>-->
  
  </div>
</template>

<script>
import ccwSelect from './../../components/qixiubao/ccwSelect/ccwSelect.vue'
import resultPanel from './../../components/qixiubao/resultPanel/resultPanel.vue'
import leftMenu from './../../components/qixiubao/leftMenu/leftMenu.vue'
import commonCondition from './../../components/qixiubao/commonCondition/commonCondition.vue'
import { Loading, Message } from 'element-ui'
import * as escData from './../../data/escData.js'
import * as evaluteData from './../../data/evaluteData.js'
import * as ccwCityData from './../../data/ccwCity.js'
import cthCity from './../../data/cthCity.js'
import escfCity from './../../data/newEscfCity.js'
import ccwCity from './../../data/newCcwCity.js'
export default {
  components: {
    ccwSelect,
    resultPanel,
    leftMenu,
    commonCondition
  },
  data() {
    return {
      showCommonSelect: false,
      dispatch: this.$store.dispatch, // 用于vuex值得动态设置
      getter: this.$store.getters,
      ccwPPList: {}, //车虫网数据
      escFPPList: {}, //二手车之家数据
      cthPPList: {}, //车300数据
      cityList: {
        ccwCity: ccwCity.ccwCity,
        cthCity: cthCity.cthCity,
        escfCity: escfCity.escfCity,
      },
      ccwCondition: {
        type: '1',
        curSimleTime: '',
        curSelect: '',
        curChildSelect: '',
        curModels: ''
      },
      escfCondition: {
        type: '3',
        curSimleTime: '',
        curSelect: '',
        curChildSelect: '',
        curModels: ''
      },
      cthCondition: {
        type: '2',
        curSimleTime: '',
        curSelect: '',
        curChildSelect: '',
        curModels: ''
      },
      commonCondition: {
        ccwProvinceSelect: '',
        ccwCitySelect: '',
        cthProvinceSelect: '',
        cthCitySelect: '',
        escfProvinceSelect: '',
        escfCitySelect: '',
        distance: '',
        selectedMonth: '',
        selectedYear: ''
      },
      ccwEvaData: {},
      cthEvaData: {},
      escfEvaData: {},
      localVimData: {}
    }
  },
  methods: {
    setCommonToNull(val) {
      this.commonCondition = {
        ccwProvinceSelect: '',
        ccwCitySelect: '',
        cthProvinceSelect: '',
        cthCitySelect: '',
        escfProvinceSelect: '',
        escfCitySelect: '',
        distance: '',
        selectedMonth: '',
        selectedYear: ''
      }
    },
    getLoading() {
      return Loading.service({
        target: this.$refs.evaluationWrapper,
        fullscreen: false,
        text: "拼命加载中~"
      })
    },
    init() {
      var _self = this
      var loading = _self.getLoading()
      var first = false
      var second = false
      var third = false

      var vuexCcwPPList = _self.getter.ccwPPList
      var vuexCthPPList = _self.getter.cthPPList
      var vuexEscfPPList = _self.getter.escfPPList

      if (vuexEscfPPList) {
        _self.escFPPList = vuexEscfPPList
        first = true
      } else {
        escData.getEscFamily(_self, (result) => {
          if (!result.error) {
            _self.dispatch('setEscfPPList', result)
            _self.escFPPList = result
          } else {
            _self.dispatch('setEscfConditionToNull', '')
            this.$message({
              message: '二手车之家数据请求失败',
              type: type,
              duration: 1000
            });
          }
          first = true
        })
      }

      if (vuexCcwPPList) {
        _self.ccwPPList = vuexCcwPPList
        second = true
      } else {
        escData.getCcwData(_self, (res) => {
          // console.log('车虫')
          // console.log(res)
          if (!res.error) {
            escData.analysisCcwData($(".zanCunData1"), res.data, (result) => {
              // console.log(result)
              if (!result.error) {
                _self.dispatch('setCcwPPList', result)
                _self.ccwPPList = result
              } else {
                _self.dispatch('setCcwConditionToNull', '')
                this.$message({
                  message: '车虫网数据请求失败',
                  type: type,
                  duration: 1000
                });
              }
              second = true
            })
          } else {
            _self.dispatch('setCcwConditionToNull', '')
            second = true
          }
        })
      }

      if (vuexCthPPList) {
        _self.cthPPList = vuexCthPPList
        third = true
      } else {
        escData.getCThreeHundred(_self, (res) => {
          if (!res.error) {
            escData.analysisCTHData($(".zanCunData2"), res.data, (result) => {
              // console.log(result)
              if (!result.error) {
                _self.dispatch('setCthPPList', result)
                _self.cthPPList = result
              } else {
                _self.dispatch('setCthConditionToNull', '')
                this.$message({
                  message: '车300数据请求失败',
                  type: type,
                  duration: 1000
                });
              }
              third = true
            })
          } else {
            _self.dispatch('setCthConditionToNull', '')
            third = true
          }
        })
      }

      var timer = setInterval(() => {
        if (first && second && third) {
          loading.close()
          clearInterval(timer)
        }
      }, 10)
    },
    getSearchCondition(val) {
      var _self = this
      var type = parseInt(val.type)
      // console.log(val)
      switch (type) {
        case 1:
          _self.dispatch('setCcwCondition', val)
          if (_self.ccwCondition.curModels && val.curModels && val.curModels.id !== _self.ccwCondition.curModels.id) {
            this.resetCommonCondition()
          }
          _self.ccwCondition = val
          break;
        case 2:
          _self.dispatch('setCthCondition', val)
          if (_self.cthCondition.curModels && val.curModels && val.curModels.id !== _self.cthCondition.curModels.id) {
            this.resetCommonCondition()
          }
          _self.cthCondition = val
          break;
        case 3:
          _self.dispatch('setEscfCondition', val)
          if (_self.escfCondition.curModels && val.curModels && val.curModels.id !== _self.escfCondition.curModels.id) {
            this.resetCommonCondition()
          }
          _self.escfCondition = val
          break;
      }
      _self.judgeShowCommonCondition()
    },
    getCommonCondition(val) {
      this.commonCondition = val
      this.dispatch('setCommonCondition', this.commonCondition)
    },
    judgeShowCommonCondition() {
      if (this.ccwCondition.curModels || this.cthCondition.curModels || this.escfCondition.curModels) {
        this.showCommonSelect = true
      } else {
        this.showCommonSelect = false
      }
    },
    resetCommonCondition() {
      this.commonCondition = {
        ccwProvinceSelect: '',
        ccwCitySelect: '',
        cthProvinceSelect: '',
        cthCitySelect: '',
        escfProvinceSelect: '',
        escfCitySelect: '',
        distance: '',
        selectedMonth: '',
        selectedYear: ''
      }
    },
    judgeInfo() {
      var _self = this
      if (!_self.ccwCondition.curModels && !_self.cthCondition.curModels && !_self.escfCondition.curModels) {
        _self.showMsgTips('您还未选择完整的汽车信息', 'warning')
        return
      }
      if (!_self.commonCondition.ccwCitySelect && !_self.commonCondition.cthCitySelect && !_self.commonCondition.escfCitySelect) {
        _self.showMsgTips('请选择上牌地区', 'warning')
        return
      }
      if (!_self.commonCondition.selectedMonth && !_self.commonCondition.selectedYear) {
        _self.showMsgTips('请选择上牌时间', 'warning')
        return
      }
      if (!_self.commonCondition.distance) {
        _self.showMsgTips('请输入行驶里程', 'warning')
        return
      }
      _self.verificationEvalute()
    },
    verificationEvalute() {
      var _self = this
      if (!_self.ccwCondition.curModels || !_self.cthCondition.curModels || !_self.escfCondition.curModels) {
        this.$confirm('您还未全部选择其他网站的车辆信息，是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _self.evalute()
        }).catch(() => {
        });
      } else {
        _self.evalute()
      }
    },
    evalute() {
      var _self = this
      var first = false
      var second = false
      var third = false

      var firstErr = false
      var secondErr = false
      var thirdErr = false

      _self.dispatch('setCCWEvaluteRsultToNull', '')
      _self.dispatch('setCTHEvaluteRsultToNull', '')
      _self.dispatch('setESCFEvaluteRsultToNull', '')

      var loading = _self.getLoading()
      if (!_self.ccwCondition.curModels) {
        first = true
      } else {
        var condition = {
          type: '1',
          curSimleTime: this.ccwCondition.curSimleTime,
          curSelect: this.ccwCondition.curSelect,
          curChildSelect: this.ccwCondition.curChildSelect,
          curModels: this.ccwCondition.curModels,
          selectedProvince: this.commonCondition.ccwProvinceSelect,
          selectedCity: this.commonCondition.ccwCitySelect,
          distance: this.commonCondition.distance,
          selectedMonth: this.commonCondition.selectedMonth,
          selectedYear: this.commonCondition.selectedYear
        }
        evaluteData.evaluteCcw($('.zanCunData1'), _self, condition, (result) => {
          _self.ccwEvaData = result
          // console.log(_self.ccwEvaData)
          if (result.error) {
            firstErr = true
            first = true
            if (secondErr && thirdErr) {
              loading.close()
              _self.showMsgTips('估价失败', 'warning')
            } else if (second && third) {
              loading.close()
              _self.$router.push({ path: '/evaluationResult' })
            }
          } else {
            _self.dispatch('setCCWEvaluteRsult', _self.ccwEvaData)
            first = true
            if (second && third) {
              loading.close()
              _self.$router.push({ path: '/evaluationResult' })
            }
          }

        })
      }

      if (!_self.cthCondition.curModels) {
        second = true
        secondErr = true
      } else {
        var condition = {
          type: '1',
          curSimleTime: this.cthCondition.curSimleTime,
          curSelect: this.cthCondition.curSelect,
          curChildSelect: this.cthCondition.curChildSelect,
          curModels: this.cthCondition.curModels,
          selectedProvince: this.commonCondition.cthProvinceSelect,
          selectedCity: this.commonCondition.cthCitySelect,
          distance: this.commonCondition.distance,
          selectedMonth: this.commonCondition.selectedMonth,
          selectedYear: this.commonCondition.selectedYear
        }
        evaluteData.evaluteCth($('.zanCunData2'), _self, condition, (result) => {
          _self.cthEvaData = result
          if (result.error) {
            secondErr = true
            second = true
            if (firstErr && thirdErr) {
              loading.close()
              _self.showMsgTips('估价失败', 'warning')
            } else if (first && third) {
              loading.close()
              _self.$router.push({ path: '/evaluationResult' })
            }
          } else {
            // console.log(_self.cthEvaData)
            _self.dispatch('setCTHEvaluteRsult', _self.cthEvaData)
            second = true
            if (first && third) {
              loading.close()
              _self.$router.push({ path: '/evaluationResult' })
            }
          }
        })
      }

      if (!_self.escfCondition.curModels) {
        third = true
        thirdErr = true
      } else {
        var condition = {
          type: '1',
          curSimleTime: this.escfCondition.curSimleTime,
          curSelect: this.escfCondition.curSelect,
          curChildSelect: this.escfCondition.curChildSelect,
          curModels: this.escfCondition.curModels,
          selectedProvince: this.commonCondition.escfProvinceSelect,
          selectedCity: this.commonCondition.escfCitySelect,
          distance: this.commonCondition.distance,
          selectedMonth: this.commonCondition.selectedMonth,
          selectedYear: this.commonCondition.selectedYear
        }
        evaluteData.evaluteEscf($('.zanCunData3'), _self, condition, (result) => {
          _self.escfEvaData = result
          if (result.error) {
            thirdErr = true
            third = true
            if (firstErr && secondErr) {
              loading.close()
              _self.showMsgTips('估价失败', 'warning')
            } else if (second && first) {
              loading.close()
              _self.$router.push({ path: '/evaluationResult' })
            }
          } else {
            // console.log(_self.escfEvaData)
            _self.dispatch('setESCFEvaluteRsult', _self.escfEvaData)
            third = true
            if (second && first) {
              loading.close()
              _self.$router.push({ path: '/evaluationResult' })
            }
          }
        })
      }

      // var timer = setInterval(() => {
      //   console.log('first = ' + first + ' second = ' + second + ' third = ' + third)
      //   console.log('first = ' + firstErr + ' second = ' + secondErr + ' third = ' + thirdErr)
      //   if (first && second && third) {
      //     loading.close()
      //     _self.$router.push({ path: '/evaluationResult' })
      //     clearInterval(timer)
      //   } else if (firstErr && secondErr && thirdErr) {
      //     loading.close()
      //     _self.showMsgTips('估价失败', 'warning')
      //     clearInterval(timer)
      //   }
      // }, 30)
    },

    showMsgTips(msg, type) {
      this.$message({
        message: msg,
        type: type,
        duration: 1000
      });
    },
    test() {
      this.$router.push({ path: '/evaluationResult' })
    }
  },
  mounted() {
    var _self = this
    _self.init()
    // _self.getCcwCity()
    // _self.getCthCity()
    // _self.getEscfCity()

    _self.ccwCondition = _self.getter.ccwCondition
    _self.cthCondition = _self.getter.cthCondition
    _self.escfCondition = _self.getter.escfCondition
    _self.commonCondition = _self.getter.commonCondition

    _self.judgeShowCommonCondition()

    var localVimData = _self.getter.VimBaseData
    if (localVimData) {
      _self.localVimData = localVimData
    }
  }
}
</script>

<style lang="less">
@import './evaluation.less';
</style>
